//package com.gis.handler;
//
//import java.util.HashMap;
//import org.springframework.web.bind.annotation.ExceptionHandler;
//import org.springframework.web.bind.annotation.RestControllerAdvice;
//import com.gis.result.ResponseResult;
//
//@RestControllerAdvice
//public class GlobalExceptionHandler {
//
//    @ExceptionHandler(Exception.class)
//    public ResponseResult<?> handleException(Exception e) {
//        // 记录异常日志
//        e.printStackTrace();
//        HashMap<String, Object> errorDetails = new HashMap<>();
//        errorDetails.put("code", "500");
//        errorDetails.put("message", e.getMessage());
//
//        // 返回错误响应
//        return ResponseResult.fail(e.getMessage(), errorDetails);
//    }
//} 
package com.gis.handler;

import com.gis.exception.ApiException;
import com.gis.result.ResponseResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ApiException.class)
    public ResponseResult<?> handleApiException(ApiException e) {
        return ResponseResult.fail(e.getCode(), e.getMessage(), null);
    }

    @ExceptionHandler(Exception.class)
    public ResponseResult<?> handleException(Exception e) {
        // 兜底异常
        return ResponseResult.fail(500, "服务器内部错误", e.getMessage());
    }
}